import sys
import os
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(SCRIPT_DIR)
import string
import os
import TranslateLang
import ProcessVideo

#需要处理文件的目录。字幕序号也一样放到相同目录下面。字幕例如 5.srt （无需和视频文件格式一样）
file_path = "Z:/nginx/爽剧/神医狂飙"
#视频文件格式其中$i是占位符
file_name_patten = "Video$i.mp4"
#开始处理视频序号
file_name_index_from = 16
#结束处理视频序号
file_name_index_to = 16
#需要转换的语言
trans_langs = ["阿拉伯","日语","韩语","泰语","越南","马来语","印地语","英语","繁体中文","西班牙","法语","德语","孟加拉","俄语","葡萄牙","乌尔都"]
#trans_langs = ["日语"]
#字幕序号y轴坐标，也就是距离视频左上角的距离
blur_yaixs = 545
#字幕虚化的高度
blur_heigh = 40
#新的字母距离视频左边边距
title_xaisx = 10
#新的字母距离左上角的距离
title_yaixs =  550
#字幕颜色
font_color = (255,255,255)
#字体目录
font_path = "C:/Windows/Fonts/"
#字幕语言配置。自定义字幕名称、
#bySpace 是否按空格换行(如果字母超过视频宽度，True是按空格换行，False是按长度换行)
#font    指定字体
#font-size 字体大小
#google_lang 谷歌翻译的语言代码 参照：https://py-googletrans.readthedocs.io/en/latest/  
LANGS = {
    "阿拉伯":       {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"ar"},
    "日语":         {"bySpace":False,   "font":font_path+"MSGOTHIC.TTC",    "font-size":24, "google_lang":"ja"},
    "韩语":         {"bySpace":True,    "font":font_path+"malgun.ttf",    "font-size":24, "google_lang":"ko"},
    "泰语":         {"bySpace":False,   "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"th"},
    "越南":         {"bySpace":True,   "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"vi"},
    "马来语":       {"bySpace":True,   "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"ms"},
    "印地语":       {"bySpace":True,    "font":font_path+"Nirmala.ttf",    "font-size":24, "google_lang":"hi"},
    "英语":         {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"en"},
    "繁体中文":     {"bySpace":False,    "font":font_path+"simhei.ttf",    "font-size":24, "google_lang":"zh-tw"},
    "西班牙":       {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"es"},
    "法语":         {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"fr"},
    "德语":         {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"de"},
    "孟加拉":       {"bySpace":True,    "font":font_path+"Nirmala.ttf",    "font-size":24, "google_lang":"bn"},
    "俄语":         {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"ru"},
    "葡萄牙":       {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"pt"},
    "乌尔都":       {"bySpace":True,    "font":font_path+"tahoma.ttf",    "font-size":24, "google_lang":"ur"}
}
force_srt = False
force_video = True

hasAllDone = False
while not hasAllDone:
    try:
        for lang_key in trans_langs:
            lang = LANGS.get(lang_key)
            #目标目录如果不存在就创建
            dest_path = file_path + "/" + lang_key
            if not os.path.exists(dest_path):
                os.makedirs(dest_path)
            i = file_name_index_from
            while i<=file_name_index_to:
                #翻译
                srt_file = file_path + "/" + str(i) + ".srt"
                dest_srt_file = dest_path + "/" + str(i) + ".srt"
                if not os.path.exists(dest_srt_file) or force_srt:
                    TranslateLang.translate_srt(srt_file, "zh-cn", "utf-8", dest_srt_file, lang.get("google_lang"), "utf-8")
                #增加字幕
                video_file_name = string.Template(file_name_patten).substitute(i=i)
                src_video_file = file_path + "/" + video_file_name
                dest_video_file = dest_path +"/video"+ str(i) + ".mp4"
                dest_video_audio_file = dest_path +"/"+ str(i) + ".mp4"
                if not os.path.exists(dest_video_audio_file) or force_video:
                    ProcessVideo.add_title(src_video_file,blur_yaixs,blur_heigh,dest_srt_file,lang.get("font"),lang.get("font-size"),font_color,title_xaisx,title_yaixs,lang.get("bySpace"),dest_video_file)
                    ProcessVideo.merge_audio(src_video_file,dest_video_file,dest_video_audio_file)    
                    os.remove(dest_video_file)
                i = i+1
        hasAllDone = True        
    except Exception as e:
        print("error")